package com.xiaoyu.lanling.feature.chat.viewholder

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import com.qmuiteam.qmui.widget.QMUILoadingView
import com.xiaoyu.base.utils.extensions.isSystemAccount
import com.xiaoyu.im.datamodel.MessageDisplayStatus
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.feature.chat.model.message.ChatMessageSendTextItem
import com.xiaoyu.lanling.widget.textview.LinkEmojiTextView

class ChatMessageSendTextViewHolder : ChatMessageBaseReceiveSendViewHolder<ChatMessageSendTextItem>() {

    private var mFail: ImageButton? = null
    private var mLoading: QMUILoadingView? = null
    private var mText: LinkEmojiTextView? = null

    override fun doCreateChildrenView(layoutInflater: LayoutInflater, parent: ViewGroup): View? {
        val view = layoutInflater.inflate(R.layout.item_chat_send_text, parent, false)
        mFail = view.findViewById(R.id.chat_send_fail)
        mLoading = view.findViewById(R.id.chat_send_loading)
        mText = view.findViewById(R.id.chat_message_text)
        return view
    }

    override fun showData(position: Int, itemData: ChatMessageSendTextItem) {
        super.showData(position, itemData)
        if (itemData.messageDisplayStatus == MessageDisplayStatus.SENDING) {
            mLoading?.visibility = View.VISIBLE
            mFail?.visibility = View.GONE
        } else if (itemData.messageDisplayStatus == MessageDisplayStatus.NORMAL_FAIL) {
            mFail?.setImageResource(R.drawable.icon_chat_send_fail)
            mFail?.visibility = View.VISIBLE
            mLoading?.visibility = View.GONE
        } else if (itemData.messageDisplayStatus == MessageDisplayStatus.BLACKED_FAIL) {
            mFail?.setImageResource(R.drawable.icon_chat_check_fail)
            mFail?.visibility = View.VISIBLE
            mLoading?.visibility = View.GONE
        } else {
            mFail?.visibility = View.GONE
            mLoading?.visibility = View.GONE
        }
        mText?.text = itemData.text
        mText?.enableClickForLink = itemData.user.isSystemAccount()
    }
}